/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.commands;

import com.SyDeck.sdCities.CityInfo;
import com.SySammy.SdBountyHunter.Handlers.BountyHandler;
import com.SySammy.SdBountyHunter.Listeners.PrisonMaker;
import com.SySammy.SdBountyHunter.sdLaw;
import com.SySammy.SdCurrency.sdCCommandExport;
import java.util.ArrayList;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class sdBountyHunterCmds implements CommandExecutor {

    sdLaw plugin;
    private sdCCommandExport CE = new sdCCommandExport();

    public sdBountyHunterCmds(sdLaw aThis) {
        this.plugin = aThis;
    }

    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("bounty")) {
            return bountyMethod(cs, Nargs);
        } else if (cmd.equals("prison")) {
            return PrisonMethod(cs, Nargs);
        }
        return true;
    }

    private boolean bountyMethod(CommandSender cs, String[] Nargs) {
        String[] args = Nargs;
        Player ply = (Player) cs;
        if (args.length <= 0) {
            try {
                BountyHandler BH = new BountyHandler(plugin);
                if (!plugin.casUseBountylist(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                ply.sendMessage(ChatColor.DARK_AQUA + "Current bounties list: ");
                ply.sendMessage(ChatColor.DARK_AQUA + "[Player, Bounty, Reason, Caught?]");
                ArrayList<ArrayList<Object>> b = BH.GetBounties();
                if (b == null || b.size() <= 0) {
                    ply.sendMessage(ChatColor.RED + "No bounties started at this moment.");
                }
                for (int x = 0; x < b.size(); x++) {
                    ply.sendMessage(ChatColor.GOLD + b.get(x).toString());
                }
                return true;
            } catch (Exception e) {
                cs.sendMessage(ChatColor.RED + "An unexpected error has occurred.");
                return false;
            }
        } else if (args[0].equalsIgnoreCase("-s")) {
            PrisonMaker CP = new PrisonMaker(plugin);
            BountyHandler BH = new BountyHandler(plugin);
            try {
                if (!plugin.casUseBountySet(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (CP.HasPrison(ply.getName()) == false) {
                    ply.sendMessage(ChatColor.RED + "You need to own a prison to make bounties.");
                    return true;
                }
                if (!CE.HasMoney(ply.getName(), Integer.parseInt(args[2]))) {
                    ply.sendMessage(ChatColor.RED + "You don't have that much money.");
                    return true;
                }
                if (BH.HasBounty(args[1]) == true) {
                    ply.sendMessage(ChatColor.RED + "That player already has a bounty running.");
                    return true;
                }

                int bounty = Integer.parseInt(args[2]);
                String badguyName = args[1].toLowerCase();
                String reason = "";
                for (int x = 3; x < args.length; x++) {
                    reason = reason.concat(args[x].toString() + " ");
                }

                CE.TakeMoney(ply.getName(), bounty);
                BH.MakeBounty(badguyName, ply.getName(), bounty, reason.trim());

                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "A " + ChatColor.GOLD + String.valueOf(bounty) + " " + CE.GetCurrency() + ChatColor.DARK_AQUA + " bounty was started on " + ChatColor.GOLD + badguyName + "'s " + ChatColor.DARK_AQUA + "head.");
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "Reason: " + ChatColor.GOLD + reason.toString());
                Player badguy = (Player) cs.getServer().getPlayer(args[1]);
                if (badguy == null) {
                    return true;
                }
                badguy.sendMessage(ChatColor.RED + "[WARNING]");
                badguy.sendMessage(ChatColor.RED + "You have a bounty on your head.");
                badguy.sendMessage(ChatColor.RED + "Reason: " + ChatColor.GOLD + reason.toString());

            } catch (Exception e) {
                cs.sendMessage(ChatColor.RED + "Input error and/or player not available.");
                return false;
            }
            return true;

        } else if (args[0].equalsIgnoreCase("-r")) {
            try {
                BountyHandler BH = new BountyHandler(plugin);
                if (!plugin.casUseBountyRemove(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                if (BH.HasBounty(args[1]) == false) {
                    ply.sendMessage(ChatColor.RED + "That player doesn't have a bounty running.");
                    return true;
                }

                String badguyName = args[1].toLowerCase();
                BH.RemoveBounty(args[1]);
                ply.getServer().broadcastMessage(ChatColor.DARK_AQUA + "The bounty on " + ChatColor.GOLD + badguyName + "'s " + ChatColor.DARK_AQUA + "head was removed.");
                Player badguy = (Player) cs.getServer().getPlayer(args[1]);
                if (badguy == null) {
                    return true;
                }
                badguy.sendMessage(ChatColor.DARK_AQUA + "[INFO]");
                badguy.sendMessage(ChatColor.DARK_AQUA + "The bounty on your head was removed.");
            } catch (Exception e) {
                cs.sendMessage(ChatColor.RED + "Input error and/or player not available.");
                return false;
            }
        } else if (args[0].equalsIgnoreCase("-h")) {
            if (!plugin.casUseBountyHelp(ply)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            return false;
        }
        return true;
    }

    private boolean PrisonMethod(CommandSender cs, String[] Nargs) {
        String[] args = Nargs;
        Player p = (Player) cs;
        if (!plugin.casUseBountyPrison(p)) {
            cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
            return true;
        }
        if (args[0].equalsIgnoreCase("-m") && args.length <= 1) {
            if (!plugin.casUseBountyPrisonMake(p)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            CityInfo ci = new CityInfo();
            if (!ci.izOwner(p.getName())) {
                p.sendMessage(ChatColor.RED + "Only city owners can make a prison.");
                return true;
            }
            PrisonMaker PM = new PrisonMaker(plugin);
            if (PM.HasPrison(p.getName())) {
                p.sendMessage(ChatColor.RED + "Your city already has a prison");
                return true;
            }

            PM.RegPrisonOwner(p, false);
            return true;
        } else if (args[0].equalsIgnoreCase("-r") && args.length <= 1) {
            if (!plugin.casUseBountyPrisonRemove(p)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            CityInfo ci = new CityInfo();
            if (!ci.izOwner(p.getName())) {
                p.sendMessage(ChatColor.RED + "Only city owners cam remove a prison.");
                return true;
            }
            PrisonMaker PM = new PrisonMaker(plugin);
            if (!PM.HasPrison(p.getName())) {
                p.sendMessage(ChatColor.RED + "Your city doesn't have a prison");
                return true;
            }
            PM.RemovePrison(p);
            p.sendMessage(ChatColor.GREEN + "Your city prison has been removed.");
            return true;
        } else if (args[0].equalsIgnoreCase("-h") && args.length <= 1) {
            if (!plugin.casUseBountyHelp(p)) {
                cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            return false;
        }
        return false;
    }
}
